package com.zxkj.commonpatrol.repoisity;

import androidx.lifecycle.MutableLiveData;

import com.allen.library.base.BaseHttpUtil;
import com.allen.library.base.BaseResponse;
import com.allen.library.bean.PostBean;
import com.allen.library.download.DownloadInterceptor;
import com.allen.library.download.DownloadObserver;
import com.allen.library.factory.ApiFactory;
import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.allen.library.utils.IntUtil;
import com.allen.library.utils.upload;
import com.czy.basecommon.utils.base.BaseRepository;
import com.czy.basecommon.utils.constant.ItrackConstant;
import com.czy.basecommon.utils.network.AppUrlConfig;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.czy.basecommon.utils.tools.GsonUtil;
import com.czy.basecommon.utils.tools.SPUtils;
import com.zxkj.commonpatrol.model.entity.SubAlarmLogHistory;
import com.zxkj.commonpatrol.model.entity.SubAlarmLogHistorys;
import com.zxkj.commonpatrol.model.entity.itemVioceEntity;
import com.zxkj.commonpatrol.model.post.PostAlarmLogInfo;
import com.zxkj.commonpatrol.model.post.PostAlarmLogMedioInfo;
import com.zxkj.commonpatrol.model.post.PostAlarmPhotoInfo;
import com.zxkj.commonpatrol.model.post.PostAlarmVedioInfo;
import com.zxkj.commonpatrol.model.post.PostAlarmVoiceInfo;
import com.zxkj.commonpatrol.model.post.PostLogInfo;
import com.zxkj.commonpatrol.model.post.PostPhoto;
import com.zxkj.commonpatrol.model.post.PostVedio;
import com.zxkj.commonpatrol.model.post.PostVoice;
import com.zxkj.commonpatrol.model.response.AlarmIdBean;
import com.zxkj.commonpatrol.model.response.MediaInfoResponse;
import com.zxkj.commonpatrol.net.ETFAPi;
import com.zxkj.commonpatrol.net.ETFApiHelper;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;

import static com.zxkj.commonpatrol.net.ETFApiHelper.BASE_URL;

public class AddExceptionRepoisity extends BaseRepository {
    public AddExceptionRepoisity() {

    }





    public void getHistoryData(String AlarmLogId, MyBaseDataCallBack<SubAlarmLogHistorys> myBaseDataCallBack){
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("LoginId", SPUtils.get(ItrackConstant.LOGINID, ""));
        map.put("Token", SPUtils.get(ItrackConstant.TOKEN, ""));
        map.put("Lang", 1);
        PostAlarmLogInfo logInfo =new PostAlarmLogInfo();
        logInfo.setAlarmLogId(AlarmLogId);
        ETFApiHelper.geRadomApi().getSubDevQuestPatrolHisoty(map,new BaseHttpUtil<PostAlarmLogInfo>().convertVo2Json(logInfo))
                .compose(Transformer.switchSchedulers())
                .subscribe(new CommonObserver<SubAlarmLogHistorys>() {
                    @Override
                    protected void onError(String errorMsg) {
                       SubAlarmLogHistorys alarmLog=new SubAlarmLogHistorys();
                        myBaseDataCallBack.onDataFailCallBack(alarmLog,alarmLog);
                    }

                    @Override
                    protected void onSuccess(SubAlarmLogHistorys alarmLog) {
                        myBaseDataCallBack.onDataSuccessCallBack(alarmLog);
                    }
                });

    }





    public void uploadPhoto(String path, String AlarmId, int GroupId, int Index,int Type, MyBaseDataCallBack myBaseDataCallBack) {
        PostPhoto postPhoto = new PostPhoto();
        if (Type==0){
            postPhoto.setAlarmId(AlarmId);
        }else{
            postPhoto.setAlarmId(AlarmId);
        }

        postPhoto.setGroupId(GroupId);
        postPhoto.setIndex(Index);
        String s1 = GsonUtil.getGson().toJson(postPhoto);
        int length = s1.getBytes().length;
        byte[] bytes = IntUtil.Int2Bytes(length);



        upload.uploadAlarmPhoto(SPUtils.get(ItrackConstant.LOGINID, ""), SPUtils.get(ItrackConstant.TOKEN, ""), ItrackConstant.BASE_URL + "api/Logs/UploadAlarmPhoto", path, s1, bytes, new upload.DataCallBack<BaseResponse>() {
            @Override
            public void onDataSuccessCallBack(BaseResponse baseResponse) {
//                ETFApp.setValue(pointCode, true);
//                uploadPhoto.delete();
                myBaseDataCallBack.onDataSuccessCallBack(baseResponse);
            }

            @Override
            public void onDataFailCallBack(BaseResponse baseResponse) {
//                ETFApp.setValue(pointCode, false);
                myBaseDataCallBack.onDataFailCallBack(404, baseResponse.getError());
            }
        });
    }

    public void uploadAlarmVoice(String path, String AlarmId, int GroupId, int Index,int Type, MyBaseDataCallBack myBaseDataCallBack) {
        PostVoice postVoice = new PostVoice();

        if (Type==0){
            postVoice.setAlarmId(AlarmId);
        }else{
            postVoice.setAlarmId(AlarmId);
        }
        postVoice.setGroupId(GroupId);
        postVoice.setIndex(Index);
        String s1 = GsonUtil.getGson().toJson(postVoice);
        int length = s1.getBytes().length;
        byte[] bytes = IntUtil.Int2Bytes(length);
        upload.uploadAlarmPhoto(SPUtils.get(ItrackConstant.LOGINID, ""), SPUtils.get(ItrackConstant.TOKEN, ""), ItrackConstant.BASE_URL + "api/Logs/UploadAlarmVoice", path, s1, bytes, new upload.DataCallBack<BaseResponse>() {
            @Override
            public void onDataSuccessCallBack(BaseResponse baseResponse) {
//                ETFApp.setValue(pointCode, true);
//                uploadPhoto.delete();
                myBaseDataCallBack.onDataSuccessCallBack(baseResponse);
            }

            @Override
            public void onDataFailCallBack(BaseResponse baseResponse) {
//                ETFApp.setValue(pointCode, false);
                myBaseDataCallBack.onDataFailCallBack(404, baseResponse.getError());
            }
        });
    }

    public void uploadAlarmVedio(String path, String AlarmId, int GroupId, int Index, MyBaseDataCallBack myBaseDataCallBack) {
        PostVedio postVedio = new PostVedio();
        postVedio.setAlarmId(AlarmId);
        postVedio.setGroupId(GroupId);
        postVedio.setIndex(Index);
        String s1 = GsonUtil.getGson().toJson(postVedio);
        int length = s1.getBytes().length;
        byte[] bytes = IntUtil.Int2Bytes(length);
        upload.uploadAlarmPhoto(SPUtils.get(ItrackConstant.LOGINID, ""), SPUtils.get(ItrackConstant.TOKEN, ""), ItrackConstant.BASE_URL + "/api/Logs/UploadAlarmVedio", path, s1, bytes, new upload.DataCallBack<BaseResponse>() {
            @Override
            public void onDataSuccessCallBack(BaseResponse baseResponse) {
//                ETFApp.setValue(pointCode, true);
//                uploadPhoto.delete();
                myBaseDataCallBack.onDataSuccessCallBack(baseResponse);
            }

            @Override
            public void onDataFailCallBack(BaseResponse baseResponse) {
//                ETFApp.setValue(pointCode, false);
                myBaseDataCallBack.onDataFailCallBack(404, baseResponse.getError());
            }
        });
    }






    public void uploadAlarmLog(String LogId,String Descr,MyBaseDataCallBack myBaseDataCallBack){
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("LoginId", SPUtils.get(ItrackConstant.LOGINID, ""));
        map.put("Token", SPUtils.get(ItrackConstant.TOKEN, ""));
        map.put("Lang", 1);

        PostAlarmLogInfo postAlarmLogInfo = new PostAlarmLogInfo();
        postAlarmLogInfo.setLogId(LogId);
        postAlarmLogInfo.setDescr(Descr);

        ETFApiHelper.geRadomApi().UploadAlarmLog(map,new BaseHttpUtil<PostAlarmLogInfo>().convertVo2Json(postAlarmLogInfo))
                .compose(Transformer.switchSchedulers())
                .subscribe(new CommonObserver<String>() {
                    @Override
                    protected void onError(String errorMsg) {
                        myBaseDataCallBack.onDataFailCallBack(404,errorMsg);
                    }

                    @Override
                    protected void onSuccess(String alarmLog) {
                        myBaseDataCallBack.onDataSuccessCallBack(alarmLog);
                    }
                });
    }






    public void uploadUnAlarmLog(String LogId,String Descr,MyBaseDataCallBack myBaseDataCallBack){
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("LoginId", SPUtils.get(ItrackConstant.LOGINID, ""));
        map.put("Token", SPUtils.get(ItrackConstant.TOKEN, ""));
        map.put("Lang", 1);

        PostAlarmLogInfo postAlarmLogInfo = new PostAlarmLogInfo();
        postAlarmLogInfo.setAlarmLogId(LogId);
        postAlarmLogInfo.setDescr(Descr);

        ETFApiHelper.geRadomApi().UpUnloadLog(map,new BaseHttpUtil<PostAlarmLogInfo>().convertVo2Json(postAlarmLogInfo))
                .compose(Transformer.switchSchedulers())
                .subscribe(new CommonObserver<String>() {
                    @Override
                    protected void onError(String errorMsg) {
                        myBaseDataCallBack.onDataFailCallBack(404,errorMsg);
                    }

                    @Override
                    protected void onSuccess(String alarmLog) {
                        myBaseDataCallBack.onDataSuccessCallBack(alarmLog);
                    }
                });
    }











   //修復子異常
    public void getFixException(String logId,MyBaseDataCallBack myBaseDataCallBack){
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("LoginId", SPUtils.get(ItrackConstant.LOGINID, ""));
        map.put("Token", SPUtils.get(ItrackConstant.TOKEN, ""));
        map.put("Lang", 1);

        ETFApiHelper.geRadomApi().getFixSubException(map,logId)
                .compose(Transformer.switchSchedulers())
                .subscribe(new CommonObserver<ResponseBody>() {
                    @Override
                    protected void onError(String errorMsg) {
                        myBaseDataCallBack.onDataFailCallBack(404,errorMsg);
                    }

                    @Override
                    protected void onSuccess(ResponseBody responseBody) {
                        myBaseDataCallBack.onDataSuccessCallBack(responseBody);
                    }
                });
    }


}
